using System.Web.Mvc;
using GoldenBicycleCompany.Core;
using SharpArch.Core.PersistenceSupport;
using SharpArch.Core.DomainModel;
using System.Collections.Generic;
using System;
using SharpArch.Web.NHibernate;
using NHibernate.Validator.Engine;
using System.Text;
using SharpArch.Web.CommonValidator;
using SharpArch.Core;

namespace GoldenBicycleCompany.Web.Controllers
{
    [HandleError]
    public class SeasonsController : Controller
    {
        public SeasonsController(IRepository<Season> seasonRepository) {
            Check.Require(seasonRepository != null, "seasonRepository may not be null");

            this.seasonRepository = seasonRepository;
        }

        [Transaction]
        public ActionResult Index() {
            IList<Season> seasons = seasonRepository.GetAll();
            return View(seasons);
        }

        [Transaction]
        public ActionResult Show(int id) {
            Season season = seasonRepository.Get(id);
            return View(season);
        }

        public ActionResult Create() {
            SeasonFormViewModel viewModel = SeasonFormViewModel.CreateSeasonFormViewModel();
            return View(viewModel);
        }

        [ValidateAntiForgeryToken]
        [Transaction]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create(Season season) {
            if (ViewData.ModelState.IsValid && season.IsValid()) {
                seasonRepository.SaveOrUpdate(season);

                TempData[ControllerEnums.GlobalViewDataProperty.PageMessage.ToString()] = 
					"The season was successfully created.";
                return RedirectToAction("Index");
            }

            SeasonFormViewModel viewModel = SeasonFormViewModel.CreateSeasonFormViewModel();
            viewModel.Season = season;
            return View(viewModel);
        }

        [Transaction]
        public ActionResult Edit(int id) {
            SeasonFormViewModel viewModel = SeasonFormViewModel.CreateSeasonFormViewModel();
            viewModel.Season = seasonRepository.Get(id);
            return View(viewModel);
        }

        [ValidateAntiForgeryToken]
        [Transaction]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(Season season) {
            Season seasonToUpdate = seasonRepository.Get(season.Id);
            TransferFormValuesTo(seasonToUpdate, season);

            if (ViewData.ModelState.IsValid && season.IsValid()) {
                TempData[ControllerEnums.GlobalViewDataProperty.PageMessage.ToString()] = 
					"The season was successfully updated.";
                return RedirectToAction("Index");
            }
            else {
                seasonRepository.DbContext.RollbackTransaction();

				SeasonFormViewModel viewModel = SeasonFormViewModel.CreateSeasonFormViewModel();
				viewModel.Season = season;
				return View(viewModel);
            }
        }

        private void TransferFormValuesTo(Season seasonToUpdate, Season seasonFromForm) {
			seasonToUpdate.Name = seasonFromForm.Name;
        }

        [ValidateAntiForgeryToken]
        [Transaction]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Delete(int id) {
            string resultMessage = "The season was successfully deleted.";
            Season seasonToDelete = seasonRepository.Get(id);

            if (seasonToDelete != null) {
                seasonRepository.Delete(seasonToDelete);

                try {
                    seasonRepository.DbContext.CommitChanges();
                }
                catch {
                    resultMessage = "A problem was encountered preventing the season from being deleted. " +
						"Another item likely depends on this season.";
                    seasonRepository.DbContext.RollbackTransaction();
                }
            }
            else {
                resultMessage = "The season could not be found for deletion. It may already have been deleted.";
            }

            TempData[ControllerEnums.GlobalViewDataProperty.PageMessage.ToString()] = resultMessage;
            return RedirectToAction("Index");
        }

		/// <summary>
		/// Holds data to be passed to the Season form for creates and edits
		/// </summary>
        public class SeasonFormViewModel
        {
            private SeasonFormViewModel() { }

			/// <summary>
			/// Creation method for creating the view model. Services may be passed to the creation 
			/// method to instantiate items such as lists for drop down boxes.
			/// </summary>
            public static SeasonFormViewModel CreateSeasonFormViewModel() {
                SeasonFormViewModel viewModel = new SeasonFormViewModel();
                
                return viewModel;
            }

            public Season Season { get; internal set; }
        }

        private readonly IRepository<Season> seasonRepository;
    }
}
